<template>
    <!-- 添加或修改公告对话框 -->
    <a-modal :title="title" v-model:visible="open" width="780px" append-to-body>
        <a-form ref="noticeRef" :model="form" :rules="rules" laba-width="80px">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="公告标题" prop="noticeTitle">
                        <a-input v-model="form.noticeTitle" placeholder="请输入公告标题" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="公告类型" prop="noticeType">
                        <a-select v-model="form.noticeType" placeholder="请选择">
                            <a-option v-for="dict in sys_notice_type" :key="dict.value" :label="dict.label"
                                :value="dict.value"></a-option>
                        </a-select>
                    </a-form-item>
                </a-col>
                <a-col :span="24">
                    <a-form-item label="状态">
                        <a-radio-group v-model="form.status">
                            <a-radio v-for="dict in sys_notice_status" :key="dict.value" :label="dict.value">{{
                                dict.label
                            }}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-col>
                <a-col :span="24">
                    <a-form-item label="内容">
                        <a-input v-model="form.noticeContent"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-modal>
</template>

<script setup name="NoticeForm">
import { addNotice, delNotice, getNotice, listNotice, updateNotice } from "@/api/system/notice";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict("sys_notice_status", "sys_notice_type");

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
    },
    rules: {
        noticeTitle: [{ required: true, message: "公告标题不能为空", trigger: "blur" }],
        noticeType: [{ required: true, message: "公告类型不能为空", trigger: "change" }]
    },
});

const { queryParams, form, rules } = toRefs(data);

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}
/** 表单重置 */
function reset() {
    form.value = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
    };
    proxy.resetForm("noticeRef");
}
function init(method, noticeId) {
    reset();
    open.value = true;
    if (method == 'add') {
        title.value = "添加公告";
    } else if (method == 'edit') {
        getNotice(noticeId).then(({ data }) => {
            form.value = data;
            title.value = "修改公告";
        });
    }
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["noticeRef"].validate(valid => {
        if (valid) {
            if (form.value.noticeId != undefined) {
                updateNotice(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addNotice(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

defineExpose({
    init
})
</script>